package com.carrental.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.response.Result;
import com.carrental.user.dto.UserInfoDTO;
import com.carrental.user.entity.UserInfo;
import com.carrental.user.service.UserInfoService;
import com.carrental.user.vo.UserInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息控制器
 */
@Tag(name = "用户信息接口")
@RestController
@RequestMapping("/info")
@RequiredArgsConstructor
public class UserInfoController {

    private final UserInfoService userInfoService;

    /**
     * 根据用户ID获取用户信息
     *
     * @param userId 用户ID
     * @return 用户信息
     */
    @Operation(summary = "根据用户ID获取用户信息")
    @GetMapping("/{userId}")
    public Result<UserInfoVO> getUserInfoByUserId(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long userId) {
        UserInfoVO userInfoVO = userInfoService.getUserInfoByUserId(userId);
        return Result.success(userInfoVO);
    }

    /**
     * 保存或更新用户信息
     *
     * @param userInfoDTO 用户信息DTO
     * @return 操作结果
     */
    @Operation(summary = "保存或更新用户信息")
    @PostMapping("/saveOrUpdate")
    public Result<Boolean> saveOrUpdateUserInfo(@Validated @RequestBody UserInfoDTO userInfoDTO) {
        boolean result = userInfoService.saveOrUpdateUserInfo(userInfoDTO);
        return Result.success(result);
    }

    /**
     * 分页查询用户信息
     *
     * @param current  当前页
     * @param size     每页大小
     * @param userInfo 查询条件
     * @return 分页结果
     */
    @Operation(summary = "分页查询用户信息")
    @GetMapping("/page")
    public Result<Page<UserInfoVO>> pageUserInfo(
            @Parameter(description = "当前页", required = true)
            @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小", required = true)
            @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "查询条件")
            UserInfo userInfo) {
        Page<UserInfo> page = new Page<>(current, size);
        Page<UserInfoVO> result = userInfoService.pageUserInfo(page, userInfo);
        return Result.success(result);
    }

    /**
     * 增加用户积分
     *
     * @param userId 用户ID
     * @param points 积分
     * @return 操作结果
     */
    @Operation(summary = "增加用户积分")
    @PostMapping("/addPoints")
    public Result<Boolean> addPoints(
            @Parameter(description = "用户ID", required = true)
            @RequestParam Long userId,
            @Parameter(description = "积分", required = true)
            @RequestParam Integer points) {
        boolean result = userInfoService.addPoints(userId, points);
        return Result.success(result);
    }

    /**
     * 更新用户信用分
     *
     * @param userId      用户ID
     * @param creditScore 信用分
     * @return 操作结果
     */
    @Operation(summary = "更新用户信用分")
    @PostMapping("/updateCreditScore")
    public Result<Boolean> updateCreditScore(
            @Parameter(description = "用户ID", required = true)
            @RequestParam Long userId,
            @Parameter(description = "信用分", required = true)
            @RequestParam Integer creditScore) {
        boolean result = userInfoService.updateCreditScore(userId, creditScore);
        return Result.success(result);
    }

    /**
     * 更新用户等级
     *
     * @param userId    用户ID
     * @param userLevel 用户等级
     * @return 操作结果
     */
    @Operation(summary = "更新用户等级")
    @PostMapping("/updateUserLevel")
    public Result<Boolean> updateUserLevel(
            @Parameter(description = "用户ID", required = true)
            @RequestParam Long userId,
            @Parameter(description = "用户等级", required = true)
            @RequestParam Integer userLevel) {
        boolean result = userInfoService.updateUserLevel(userId, userLevel);
        return Result.success(result);
    }
} 